package com.ruoyi.appointment.service.impl;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.appointment.domain.BusStatement;
import com.ruoyi.appointment.domain.vo.BusAppointmentVo;
import com.ruoyi.appointment.enums.AppointmentStatusEnum;
import com.ruoyi.appointment.service.IBusStatementService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.appointment.mapper.BusAppointmentMapper;
import com.ruoyi.appointment.domain.BusAppointment;
import com.ruoyi.appointment.service.IBusAppointmentService;
import org.springframework.util.Assert;

/**
 * 预约
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-14
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService {
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private ISysConfigService sysconfigService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IBusStatementService busStatementService;


    /**
     * 查询预约
     *
     * @param id 预约
     *           主键
     * @return 预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id) {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询预约
     * 列表
     *
     * @param busAppointment 预约
     * @return 预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment) {
        List<BusAppointment> list = busAppointmentMapper.selectBusAppointmentList(busAppointment);
//        System.out.println(list);
        return list;
    }

    /**
     * 新增预约
     *
     * @param busAppointmentVo 预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointmentVo busAppointmentVo) {
//        Assert.notNull(busAppointmentVo.getPhone(), "s手机号不能是空的");
//        Assert.state(ValidateUtils.validPhone(busAppointmentVo.getPhone()), "手机号格式错误");
//        Assert.notNull(busAppointmentVo.getLicensePlate(), "车牌号不能为空");
//        Assert.state(ValidateUtils.validLicensePlate(busAppointmentVo.getLicensePlate()), "车牌号格式错误");
        String maxAppointment = sysconfigService.selectConfigByKey("bus.appointment.max");
        if (StringUtils.isEmpty(maxAppointment)) {
            maxAppointment = "5";
        }
        Integer maxNum = Integer.valueOf(maxAppointment);
        String key = "bus_appointment_num" + busAppointmentVo.getPhone();
        Long seconds = DateUtils.getNowToLastSeconds();
        Long increment = redisCache.increment(key, 1, seconds, TimeUnit.SECONDS);
        Assert.state(increment <= maxNum, "当前手机号已到当天最大预约次数");
        BusAppointment busAppointment = new BusAppointment();
        BeanUtils.copyBeanProp(busAppointment, busAppointmentVo);
        busAppointment.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }

    /**
     * 修改预约
     *
     * @param busAppointmentVo 预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointmentVo busAppointmentVo) {
        BusAppointment busAppointment2 = this.selectBusAppointmentById(busAppointmentVo.getId());
        Assert.notNull(busAppointment2, "参数有误");
        Assert.state(busAppointment2.getAppointTime().getTime() > DateUtils.getNowDate().getTime(), "预约时间要大于现在");
        Assert.state(busAppointment2.getStatus() == AppointmentStatusEnum.ARRIVALING.ordinal(), "只有预约中能修改");
        BusAppointment busAppointment = new BusAppointment();
        BeanUtils.copyBeanProp(busAppointment, busAppointmentVo);
        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除预约
     *
     * @param ids 需要删除的预约
     *            主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids) {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除预约
     * 信息
     *
     * @param id 预约
     *           主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id) {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    /**
     * 已到店
     *
     * @param id
     * @return结果
     */
    @Override
    public int updateArrival(Long id) {
        BusAppointment busAppointment = selectBusAppointmentById(id);
        Assert.notNull(busAppointment, "参数有误");
        Assert.state(AppointmentStatusEnum.ARRIVALING.ordinal() == busAppointment.getStatus(), "只有预约中能设置到店");

        return busAppointmentMapper.updateArrival(id);
    }

    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @Override
    public int updateCancel(Long id) {
        BusAppointment busAppointment = selectBusAppointmentById(id);
        Assert.notNull(busAppointment, "参数有误");
        Assert.state(AppointmentStatusEnum.ARRIVALING.ordinal() == busAppointment.getStatus(), "只有预约中能设置到店");

        return busAppointmentMapper.updateStatus(id, AppointmentStatusEnum.CANCEL.ordinal());
    }

    /**
     * 生成结算单
     *
     * @param id
     * @return
     */
    @Override
    public BusStatement doCreateStatement(Long id) {
        BusAppointment busAppointment = selectBusAppointmentById(id);
        List<Integer> status = Arrays.asList(AppointmentStatusEnum.ARRIVAL.ordinal(), AppointmentStatusEnum.STATEMENT.ordinal(), AppointmentStatusEnum.PAID.ordinal());

        Assert.state(status.contains(busAppointment.getStatus().intValue()), "该状态无法结算");
        BusStatement busStatement = busStatementService.selectBusStatementByAppointmentId(id);
        if (busStatement == null) {
            busStatement = this.buildStatement(busAppointment);
            busStatementService.insertBusStatement(busStatement, id);
        }
        busAppointmentMapper.updateStatus(id, AppointmentStatusEnum.STATEMENT.ordinal());
        return busStatement;
    }

    /**
     * 根据预约信息id更改状态
     * @param appointmentId
     * @return
     */
    @Override
    public int updatePaySuccess(Long appointmentId) {
       return  busAppointmentMapper.updateStatus(appointmentId,AppointmentStatusEnum.PAID.ordinal());


    }

    /**
     * 根据用户信息创建一个结算单
     *
     * @param busAppointment
     * @return
     */
    public BusStatement buildStatement(BusAppointment busAppointment) {
        BusStatement busStatement = new BusStatement();
        busStatement.setTotalQuantity(0);
        busStatement.setTotalMount(BigDecimal.ZERO);
        busStatement.setDiscountAmount(BigDecimal.ZERO);
        busStatement.setAppointmentId(busAppointment.getId());
        busStatement.setIsDelete(0);
        busStatement.setStatus(BusStatement.CONSUMER);
        busStatement.setActualArrivalTime(busAppointment.getArrivalTima());
        busStatement.setCarSeries(busAppointment.getCarType());
        busStatement.setCustomerName(busAppointment.getUsername());
        busStatement.setCustomerPhone(busAppointment.getPhone());
        busStatement.setInfo(busAppointment.getInfo());
        busStatement.setLicensePlate(busAppointment.getLicensePlate());
        busStatement.setServiceType(busAppointment.getServiceType().intValue());
        return busStatement;
    }
}
